import java.io.PrintStream;
import java.util.HashMap;

/**
 * Created by thoughtworker on 1/16/14.
 */
public class mainMenu {

    PrintStream printStream;
    InputReader inputReader;
    Catalog catalog;

    HashMap<String, MenuAction> options;

    public mainMenu(PrintStream printStream, InputReader inputReader, Catalog catalog, HashMap<String, MenuAction> options){
        this.printStream = printStream;
        this.inputReader = inputReader;
        this.catalog = catalog;
        this.options = options;
    }

    public void run(){
        listOptions();
        executeUserChoice(inputReader.getInput());
    }

    public void listOptions(){
        printStream.println("Options:");
        for(String option : options.keySet())
            printStream.println("Type '" + option +"' to " + options.get(option).toString());
    }

    private void executeUserChoice(String input) {
        if( isValid(input) ){
            options.get(input).performAction();
        }
        else{
            printStream.println("invalid user choice! please type another command");
            run();
        }
    }

    private boolean isValid(String input) {
        for(String option : options.keySet()){
            if (input.equals(option))
                return true;
        }
        return false;
    }
}
